package com.woke.adapter;

import android.content.Context;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.netease.nim.uikit.GlideApp;
import com.woke.data.UnionPayChannel;
import com.zhongcai.online.R;

import java.util.List;

/**
 * 作者 ： 暗网
 * 创建时间：2017/3/12
 * 备注：
 */

public class UnionPayChannelAdapter extends RecyclerView.Adapter<UnionPayChannelAdapter.ViewHolder> {

	private LayoutInflater mInflater;
	private Context mContext;
	private List<UnionPayChannel> mChannels;
	private int way = - 1;
	private int selectionPosition = 0;

	private int kind;//1微信 2 支付宝  默认图标的时候使用

	public UnionPayChannelAdapter(Context context, List<UnionPayChannel> list) {
		mInflater = LayoutInflater.from (context);
		this.mContext = context;
		this.mChannels = list;
		selectionPosition = 0;

	}

	public void setChannelType(int kind) {
		this.kind = kind;
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		return new ViewHolder (mInflater.inflate (R.layout.adapter_unionpay_channel, parent, false));
	}

	@Override
	public void onBindViewHolder(ViewHolder holder, final int position) {
		final UnionPayChannel channel = mChannels.get (position);
		Log.e ("---", "selection  == " + selectionPosition + "kind = " + kind);
		if (channel != null) {
			String imgPath = channel.getIcon ();
			holder.mTitle.setText (channel.getTitle ());
			if (! TextUtils.isEmpty (imgPath)) {
				if (! imgPath.startsWith ("http")) {
					imgPath = "http://" + imgPath;
				}
				if (kind == 1) {
					GlideApp.with (mContext)
							.load (imgPath)
							.placeholder(R.mipmap.icn__launch_oem)
							.error(R.mipmap.icn__launch_oem)
							.into (holder.mIcon);
				} else if (kind == 2) {
					GlideApp.with (mContext)
							.load (imgPath)
							.placeholder(R.mipmap.icn__launch_oem)
							.error(R.mipmap.icn__launch_oem)
							.into (holder.mIcon);
				} else {
					GlideApp.with (mContext)
							.load (imgPath)
							.placeholder(R.mipmap.icn__launch_oem)
							.error(R.mipmap.icn__launch_oem)
							.into (holder.mIcon);
				}
			}
			holder.mChannelWayCb.setOnCheckedChangeListener (null);
			if (selectionPosition == position) {
				way = channel.getWay ();
				holder.mChannelWayCb.setChecked (true);
			} else {
				holder.mChannelWayCb.setChecked (false);
			}
			holder.mChannelWayCb.setOnCheckedChangeListener (new CompoundButton.OnCheckedChangeListener () {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						selectionPosition = position;
						way = channel.getWay ();
						notifyDataSetChanged ();
					}
				}
			});
			holder.itemView.setOnClickListener (new View.OnClickListener () {
				@Override
				public void onClick(View v) {
					selectionPosition = position;
					way = channel.getWay ();
					notifyDataSetChanged ();
				}
			});

		}

	}

	@Override
	public int getItemCount() {
		return mChannels == null ? 0 : mChannels.size ();
	}

	protected class ViewHolder extends RecyclerView.ViewHolder {

		private ImageView mIcon;
		private TextView mTitle;
		private AppCompatCheckBox mChannelWayCb;

		public ViewHolder(View itemView) {
			super (itemView);
			mIcon = (ImageView) itemView.findViewById (R.id.channel_img);
			mTitle = (TextView) itemView.findViewById (R.id.channel_title);
			mChannelWayCb = (AppCompatCheckBox) itemView.findViewById (R.id.channel_cb);

		}
	}

	public int getWay() {
		return way;
	}
}
